<?php

namespace App\Http\Controllers;

use App\Item;
use Cookie;
use Illuminate\Support\Facades\Response;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesResources;
use Illuminate\Support\Facades\Cache;
use App\Type;
use DB;

class Controller extends BaseController
{
    public function __construct()
    {
        if(Cache::has('nav')){
            $navs = Cache::get('nav');
        } else {
            $items = config('site.items');
            foreach ($items as $itemName => $item) {
                $navs[$itemName]['nav'] = $item['nav'];
                $navs[$itemName]['name'] = $item['name'];
                $navs[$itemName]['itemName'] = $itemName;
                $navs[$itemName]['nav'] = $item['nav'];
                if (1 == $item['nav']) {
                    if (isset($item['type'])) {
                        if ($item['data'][$item['type']]['type'] == "dynamic") {
                            $navs[$itemName]['list'] = Item::where('type', $item['data'][$item['type']]['value'])->orderBy('name','DESC')->lists('name', 'id');
                        } else {
                            $navs[$itemName]['list'] = $item['data'][$item['type']]['value'];
                        }
                    } else {
                        $navs[$itemName]['list'] = Item::where('type', $itemName)->lists('name', 'id');
                    }
                }
            }
            Cache::forever('nav',$navs);
        }
//        $list = DB::select('select b.dir,b.sub_dir,b.pic from configs a LEFT JOIN pictures b on a.val=b.id where type=1 and name like \'%indexswiper%\'');
//        print_r($list);
//        return;
        //修改首页幻灯片
        if(!Cache::has('indexswiper')){
            Cache::rememberForever('indexswiper',function(){
                $list = DB::select('select val,link from configs where name like \'%indexswiper%\'');
                return $list;
            });
        }

        if(!Cache::has('background')){
            Cache::rememberForever('background', function(){
                return DB::select('select * from configs where type=1 and `key`=\'background\'');
            });
        }
        if(!Cache::has('aboutmenu')){
            Cache::rememberForever('aboutmenu', function(){
                return DB::table('items')->select('id','name')->where('type', 'about')->where('lang', $this->setLang())->get();
            });
        }
        view()->share('aboutmenu', Cache::get('aboutmenu'));
//        print_r(Cache::get('background')); exit();
        view()->share('background_image', Cache::get('background'));
        view()->share('navs',$navs);
        //语言判断
        view()->share('lang',$this->setLang());
        //获取产品一级分类
        view()->share('Parents',$this->getParents());
        //网站设置
        view()->share('configs', Cache::get('configs'));
    }

    //语言判断
    public function setLang(){
        $lang = Cookie::get('lang');
        if($lang != 'zh_cn'){
            $lang = 'en';
        }
        Cookie::queue('lang', $lang, 200);
        \App::setLocale($lang);
        return $lang;
    }

    private function getParents()
    {
        if('' == $this->setLang()){
            Cache::forget('parents');
        }
        if (Cache::has('parents')) {
            $parents = Cache::get('parents');
        } else {
            if('zh_cn' != $this->setLang()){
                Cache::forget('parents');
                $parents = Type::where('parent_id', 0)->orderBy("range", "ASC")->lists('name', 'id');
            } else {
                $parents = Type::where('parent_id', 0)->orderBy("range", "ASC")->lists('name_cn', 'id');
            }
            Cache::forever('parents', $parents);
        }
        return $parents;
    }

    public function getAllTypes($type = 0)
    {
        if (Cache::has('AllTypes') && Cache::has('parents')) {
            $AllTypes = Cache::get('AllTypes');
        } else {
            $parents = $this->getParents();
            $AllTypes = [];
            foreach ($parents as $key => $parent) {
                $AllTypes[$key]['name'] = $parent;
            }
        }

        if (0 != $type) {
            if (!isset($AllTypes[$type]['data'])) {
                if('zh_cn' != $this->setLang()){
                    $AllTypes[$type]['data'] = Type::where('parent_id', $type)->orderBy('range','ASC')->lists('name', 'id');
                } else {
                    $AllTypes[$type]['data'] = Type::where('parent_id', $type)->orderBy('range','ASC')->lists('name_cn', 'id');
                }
                Cache::forever('AllType', $AllTypes);
            }
            return $AllTypes[$type];
        } else {
            foreach($AllTypes as $key=>$AllType){
                if (!isset($AllType['data'])) {
                    if('zh_cn' != $this->setLang()){
                        $AllTypes[$key]['data'] = Type::where('parent_id', $key)->orderBy('range','ASC')->lists('name', 'id');
                    } else {
                        $AllTypes[$key]['data'] = Type::where('parent_id', $key)->orderBy('range','ASC')->lists('name_cn', 'id');
                    }
                    Cache::forever('AllTypes', $AllTypes);
                }
            }
            return $AllTypes;
        }
    }

    use AuthorizesRequests, AuthorizesResources, DispatchesJobs, ValidatesRequests;
}